package cn.iocoder.an.module.business.controller.admin.storeinitbackup;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.business.controller.admin.storeinitbackup.vo.*;
import cn.iocoder.an.module.business.dal.dataobject.storeinitbackup.StoreInitBackupDO;
import cn.iocoder.an.module.business.service.storeinitbackup.StoreInitBackupService;

@Tag(name = "管理后台 - 库存台账备份")
@RestController
@RequestMapping("/business/store-init-backup")
@Validated
public class StoreInitBackupController {

    @Resource
    private StoreInitBackupService storeInitBackupService;

    @PostMapping("/create")
    @Operation(summary = "创建库存台账备份")
    @PreAuthorize("@ss.hasPermission('business:store-init-backup:create')")
    public CommonResult<Long> createStoreInitBackup(@Valid @RequestBody StoreInitBackupSaveReqVO createReqVO) {
        return success(storeInitBackupService.createStoreInitBackup(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新库存台账备份")
    @PreAuthorize("@ss.hasPermission('business:store-init-backup:update')")
    public CommonResult<Boolean> updateStoreInitBackup(@Valid @RequestBody StoreInitBackupSaveReqVO updateReqVO) {
        storeInitBackupService.updateStoreInitBackup(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除库存台账备份")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:store-init-backup:delete')")
    public CommonResult<Boolean> deleteStoreInitBackup(@RequestParam("id") Long id) {
        storeInitBackupService.deleteStoreInitBackup(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除库存台账备份")
                @PreAuthorize("@ss.hasPermission('business:store-init-backup:delete')")
    public CommonResult<Boolean> deleteStoreInitBackupList(@RequestParam("ids") List<Long> ids) {
        storeInitBackupService.deleteStoreInitBackupListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得库存台账备份")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:store-init-backup:query')")
    public CommonResult<StoreInitBackupRespVO> getStoreInitBackup(@RequestParam("id") Long id) {
        StoreInitBackupDO storeInitBackup = storeInitBackupService.getStoreInitBackup(id);
        return success(BeanUtils.toBean(storeInitBackup, StoreInitBackupRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得库存台账备份分页")
    @PreAuthorize("@ss.hasPermission('business:store-init-backup:query')")
    public CommonResult<PageResult<StoreInitBackupRespVO>> getStoreInitBackupPage(@Valid StoreInitBackupPageReqVO pageReqVO) {
        PageResult<StoreInitBackupDO> pageResult = storeInitBackupService.getStoreInitBackupPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StoreInitBackupRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出库存台账备份 Excel")
    @PreAuthorize("@ss.hasPermission('business:store-init-backup:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportStoreInitBackupExcel(@Valid StoreInitBackupPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StoreInitBackupDO> list = storeInitBackupService.getStoreInitBackupPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "库存台账备份.xlsx", "数据", StoreInitBackupRespVO.class,
                        BeanUtils.toBean(list, StoreInitBackupRespVO.class));
    }

}